//实现组件自动以.js文件的文件名去注册组件
import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
import camelCase from 'lodash/camelCase'
Vue.use(VueAxios, axios)
const requireComponentAxios = require.context(
  // 其组件目录的相对路径
  '../js/axios-api',
  // 是否查询其子目录里的vue文件
  true, /\.js/
)
const requireComponenthybrid = require.context(
  // 其组件目录的相对路径
  '../js/hybrid-api',
  // 是否查询其子目录里的vue文件
  true, /\.js/
)
const requireComponent = [requireComponentAxios, requireComponenthybrid]
const axiosApi = {};
const hybridApi = {};
for (let key in requireComponent) {
  requireComponent[key].keys().forEach(fileName => {
    // 获取组件配置
    const Config = requireComponent[key](fileName)
    let i = fileName.lastIndexOf("/");
    // 获取组件的 小驼峰 命名
    const componentName = camelCase(
      // 剥去文件名开头的 `./` 和结尾的扩展名
      fileName.slice(i + 1, -3).replace(/^\.\/(.*)\.\w+$/, '$1')
    )
    if (requireComponent[key] == requireComponentAxios) {
      axiosApi[componentName] = Config.default || Config

    } else {
      hybridApi[componentName] = Config.default || Config
    }

  })
}
if (process.env.NODE_ENV === 'production') {
  //build
} else {
  //dev
  for (var apiName in hybridApi) {
    // console.log(apiName)
    hybridApi[apiName] = function () {
      console.log("调用原生接口")
    }
  }
}
Vue.prototype.$axiosApi = axiosApi;
Vue.prototype.$hybridApi = hybridApi;

// console.log('api',axiosApi)
